// pages/mine/mine.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['女', '男' ],
    index:1,
    image:'',
    nickname:'',
    phone:'',
    school_name:'',
    sex:'',
    username:''
  },
  quit:function(){
      wx.removeStorage({
          key: 'yzuser',
          success: function(res) {
              wx.showToast({
                  title: '退出成功',
                  icon:"none",
                  duration:2000,
                  success:function(){
                      wx.reLaunch({
                          url: '../yzlogin/login',
                      })
                  }
              })
          },
      })
  },
  xgmm:function(){
    wx.navigateTo({
      url: '../yzxgmm/xgmm',
    })
  },
  xgxm:function(){
    wx.navigateTo({
      url: '../yzxg/xg',
    })
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  // 更换头像
  headerimg:function(){
    var that = this
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'],
      success(res) {
         console.log(res);
          wx.getStorage({
            key: 'yzuser',
            success: function(rrr) {
              console.log(rrr);
              let setdata = {
                token:rrr.data.token
              }
              console.log(setdata);
              console.log(res.tempFilePaths[0]);
              wx.uploadFile({
                url: app.api + app.yzimage, //仅为示例，非真实的接口地址
                filePath: res.tempFilePaths[0],
                name: 'image',
                formData: setdata,
                success:function(arr) {
                  var data = JSON.parse(arr.data);
                  console.log(data)
                  if (data.code == 0) {
                  //   // 改变缓存
                    var user = rrr.data;
                    user.image = res.tempFilePaths[0]
                    wx.setStorageSync('yzuser', user)
                    that.setData({
                      image: res.tempFilePaths[0]
                    });
                  //   $.alert(data.msg)
                    wx.showToast({
                      title: data.msg,
                      icon: 'none',
                      duration: 2000
                    })
                  } else {
                    wx.showToast({
                      title: data.msg,
                      icon: 'none',
                      duration: 2000
                    })
                  }
                },
              })
            },
          })

      }
    })



  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideTabBar({})
    var that = this
    wx.getStorage({
      key: 'yzuser',
      success: function(res) {
        console.log(res);
        that.setData({
          image: res.data.image,
          nickname: res.data.realname,
          phone: res.data.phone,
          school_name: res.data.schoolname,
          sex: res.data.sex,
          username:res.data.username
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.hideHomeButton()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },


  // tabbar
  goclass: function () {
    wx.reLaunch({
      url: '../yzclass/index'
    })
  },
  gohs: function () {
    wx.reLaunch({
      url: '../hs/hs'
    })
  },
  goindex: function () {
    wx.reLaunch({
      url: '../yzindex/index'
    })
  }
})